﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.AdminCP.Class;
using MLM.Data;
using Telerik.Web.UI;

namespace MLM.AdminCP.Pages
{
    public partial class MyBonus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //start year is 2013
                int thisYear = Convert.ToInt32(System.DateTime.Now.Year);
                for (int i = 2013; i <= thisYear + 20; i++)
                {
                    FromYearCbb.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
                    ToYearCbb.Items.Add(new RadComboBoxItem(i.ToString(), i.ToString()));
                }
            }
            CalculateBonusBtn.Click += new EventHandler(CalculateBonusBtn_Click);
        }

        void CalculateBonusBtn_Click(object sender, EventArgs e)
        {

            MLMDataContext ct = new MLMDataContext();
            CarrierBonusByYearRpt.DataSource = GetCarrierBonus(ct);
            CarrierBonusByYearRpt.DataBind();
            YearBonusRpt.DataSource = GetYearBonus(ct);
            YearBonusRpt.DataBind();
        }
        /// <summary>
        /// cong thuc: 
        /// lap theo tung thang trong nam, lay ra pv cua cac sponsor. cac sponsor co pv thang >4000 se dua vao 1 list. sau do count list de tra ve so % bonus
        /// </summary>
        /// <returns></returns>
        public List<BonusByYear> GetCarrierBonus(MLMDataContext ct)
        {
            List<BonusByYear> listBonus = new List<BonusByYear>();
            long userId = Permission.GetCurrentUserId();            
            var sponsorList = ct.Users.Where(it => it.SponsorID == userId).ToList();
            var startYear = int.Parse(FromYearCbb.SelectedValue);
            var endYear = int.Parse(ToYearCbb.SelectedValue);

            for (int i = 0; i <= endYear - startYear; i++)
            {
                BonusByYear bonusYear = new BonusByYear();
                bonusYear.Year = startYear + i;
                bonusYear.Bonus = new List<BonusByMonth>();
                for (int j = 1; j < 13; j++)
                {
                    var CarrierBonusConfig = ct.CarrierBonusConfigs.OrderByDescending(it => it.NumberOfRankRequire);
                    BonusByMonth bonus = new BonusByMonth();
                    bonus.Month = j;
                    var startDay = new DateTime(startYear + i, j, 1, 0, 0, 0);// ngay bat dau 1 thang
                    var endDay = new DateTime(startYear + i, j, DateTime.DaysInMonth(startYear + i, j), 23, 59, 0); // ngay ket thuc 1 thang
                    foreach (var bonusConfig in CarrierBonusConfig)
                    {
                        var salesHeadCount = 0;

                        //my point
                        long myPoint = 0;
                        foreach (var item in ct.Invoices.Where(p => p.UserID == userId && p.Status == 1 && p.DateTimeCreated >= startDay && p.DateTimeCreated <= endDay))//datetimecreated...
                        {
                            var _point = ct.InvoiceDetails.Where(p => p.InvoiceID == item.InvoiceID);
                            if (_point.Count() > 0)
                            {
                                foreach (var a in _point)
                                {
                                    myPoint += long.Parse(a.Point.ToString()) * long.Parse(a.Quantity.ToString());
                                }
                            }
                        }
                        //sponsor point
                        foreach (var user in sponsorList)
                        {
                            long point = 0;
                            foreach (var item in ct.Invoices.Where(p => p.UserID == user.UserId && p.Status == 1 && p.DateTimeCreated >= startDay && p.DateTimeCreated <= endDay))//datetimecreated...
                            {
                                var _point = ct.InvoiceDetails.Where(p => p.InvoiceID == item.InvoiceID);
                                if (_point.Count() > 0)
                                {
                                    foreach (var a in _point)
                                    {
                                        point += long.Parse(a.Point.ToString()) * long.Parse(a.Quantity.ToString());
                                    }
                                }
                            }
                            myPoint += point;
                            bonus.PV = point;
                            if (point >= ct.Ranks.FirstOrDefault(it => it.RankID == bonusConfig.RankRequire).MinPV)
                            {
                                salesHeadCount++;
                            }
                        }
                        if (myPoint >= bonusConfig.MinPVRequire)
                        {
                            if (salesHeadCount >= bonusConfig.NumberOfRankRequire)
                            {
                                bonus.Bonus = bonusConfig.Bonus;
                                break;
                            }
                            else
                            {
                                bonus.Bonus = 0;
                            }
                        }
                        else
                        {
                            bonus.Bonus = 0;//0%
                        }
                    }
                    bonusYear.Bonus.Add(bonus);
                }
                listBonus.Add(bonusYear);
            }
            return listBonus;
        }
        public List<YearBonus> GetYearBonus(MLMDataContext ct)
        {
            List<YearBonus> listYearBonus = new List<YearBonus>();
            var startYear = int.Parse(FromYearCbb.SelectedValue);
            var endYear = int.Parse(ToYearCbb.SelectedValue);
            long userId = Permission.GetCurrentUserId();
            var sponsorList = ct.Users.Where(it => it.SponsorID == userId).ToList();
            var bonusConfig = ct.YearBonusConfigs.OrderByDescending(it => it.MinPVPerYear);
            for (int i = 0; i <= endYear - startYear; i++)
            {
                YearBonus yearBonus = new YearBonus();
                yearBonus.Year = startYear + i;
                var startDay = new DateTime(startYear + i, 1, 1, 0, 0, 0);// ngay bat dau 1 nam
                var endDay = new DateTime(startYear + i, 12, DateTime.DaysInMonth(startYear + i, 12), 23, 59, 0); // ngay ket thuc 1 nam
                foreach (var yearBonusConfig in bonusConfig)
                {
                    long myPoint = 0;
                    foreach (var item in ct.Invoices.Where(p => p.UserID == userId && p.Status == 1 && p.DateTimeCreated >= startDay && p.DateTimeCreated <= endDay))//datetimecreated...
                    {
                        var _point = ct.InvoiceDetails.Where(p => p.InvoiceID == item.InvoiceID);
                        if (_point.Count() > 0)
                        {
                            foreach (var a in _point)
                            {
                                myPoint += long.Parse(a.Point.ToString()) * long.Parse(a.Quantity.ToString());
                            }
                        }
                    }
                    //sponsor point
                    foreach (var user in sponsorList)
                    {
                        long point = 0;
                        foreach (var item in ct.Invoices.Where(p => p.UserID == user.UserId && p.Status == 1 && p.DateTimeCreated >= startDay && p.DateTimeCreated <= endDay))//datetimecreated...
                        {
                            var _point = ct.InvoiceDetails.Where(p => p.InvoiceID == item.InvoiceID);
                            if (_point.Count() > 0)
                            {
                                foreach (var a in _point)
                                {
                                    point += long.Parse(a.Point.ToString()) * long.Parse(a.Quantity.ToString());
                                }
                            }
                        }
                        myPoint += point;
                    }
                    if (myPoint >= yearBonusConfig.MinPVPerYear)
                    {
                        yearBonus.Bonus = yearBonusConfig.Bonus;
                        break;
                    }
                    else
                    {
                        yearBonus.Bonus = 0;//%
                    }
                }
                listYearBonus.Add(yearBonus);
            }
            return listYearBonus;
        }
    }   
}